/*
 * Copyright (C) 2004, Velocityme Partnership
 * StatusSessionBean.java
 *
 * Created on 13 March 2004, 02:57
 */

package com.velocityme.session;

import com.velocityme.entity.ChangeDeltaItemTypeBean;
import com.velocityme.entity.ChangeDeltaTypeBean;
import com.velocityme.entity.PermissionBean;
import javax.ejb.*;
import javax.naming.*;
import java.util.*;
import com.velocityme.interfaces.*;
import com.velocityme.utility.InvalidKeyException;
import com.velocityme.utility.PermissionDeniedException;
import com.velocityme.valueobjects.*;
import java.rmi.RemoteException;

/**
 *
 * @author  Robert
 * @ejb.bean
 *           type="Stateless"
 *           cmp-version="2.x"
 *           name="StatusSession"
 *           jndi-name="ejb/StatusSession"
 *           view-type="local"
 *           transaction-type="Container"
 * @ejb.transaction type="Required"
 *
 * @ejb.util generate="physical"
 */
public class StatusSessionBean implements SessionBean {
    
    private SessionContext m_context;
    
    private SearchSessionLocal m_searchSessionLocal;

    private PermissionLocal m_permissionStatusCreate;
    private PermissionLocal m_permissionStatusEdit;
    private PermissionLocal m_permissionNodeView;

    /**
     * If the user has permission to create a new Status in the specified parent
     * node then go ahead and create it and log an appropriate ChangeDelta.
     *
     * @param p_key User's security key
     * @param p_parentNode The node which will contain the new Statue
     * @param p_statusValue The value details for the Statue part of the status
     *
     * @ejb.interface-method view-type="local"
     **/
    public StatusLocal createStatus(KeySessionLocal p_keyLocal,
                                    NodeLocal p_parentNodeLocal,
                                    com.velocityme.valueobjects.StatusValue p_statusValue)
        throws PermissionDeniedException, InvalidKeyException {
        try {
            if (m_searchSessionLocal.getAssignedPermissionsLocal(p_keyLocal, p_parentNodeLocal).contains(m_permissionStatusCreate)) {
                p_statusValue.getNodeValue().setType("Status");
                p_statusValue.getNodeValue().setFinalType("Status");
                DBObjectLocal dBObjectLocal = DBObjectUtil.getLocalHome().create();
                NodeValue nodeValue = p_statusValue.getNodeValue();
                NodeLocal nodeLocal = NodeUtil.getLocalHome().create(dBObjectLocal, nodeValue, p_parentNodeLocal);
                nodeValue.setNodeId(nodeLocal.getNodeId());
                StatusLocal statusLocal = StatusUtil.getLocalHome().create(nodeLocal, p_statusValue);
                ChangeDeltaLocal changeDeltaLocal = ChangeDeltaUtil.getLocalHome().create(new Date(), ChangeDeltaTypeBean.CREATE, p_keyLocal.getUserLocal(), dBObjectLocal);
                changeDeltaLocal.addItem(ChangeDeltaItemTypeBean.NAME, nodeValue.getName());
                changeDeltaLocal.addItem(ChangeDeltaItemTypeBean.DESCRIPTION, nodeValue.getDescription());
                changeDeltaLocal.addItem(ChangeDeltaItemTypeBean.PARENT_NODE, p_parentNodeLocal.getPathName());
                return statusLocal;
            }
            else
                throw new PermissionDeniedException();
        }
        catch(NamingException e) {
            throw new EJBException(e);
        }
        catch(CreateException e) {
            throw new EJBException(e);
        }
    }
    
    /**
     * If the user has permission to edit the Status then do so and log the
     * changes with an appropriate ChangeDelta.
     *
     * @param p_key User's security key
     * @param p_status The Status object being editted
     * @param p_statusValue The value details for the Status part of the status
     *
     * @ejb.interface-method view-type="local"
     **/
    public void editStatus(KeySessionLocal p_keyLocal,
                           StatusLocal p_statusLocal,
                           com.velocityme.valueobjects.StatusValue p_statusValue)
        throws PermissionDeniedException, InvalidKeyException {
        try {
            NodeLocal nodeLocal = p_statusLocal.getNodeLocal();
            if (m_searchSessionLocal.getAssignedPermissionsLocal(p_keyLocal, nodeLocal).contains(m_permissionStatusEdit)) {
                NodeValue nodeValue = p_statusValue.getNodeValue();
                ChangeDeltaLocal changeDeltaLocal = ChangeDeltaUtil.getLocalHome().create(new Date(), ChangeDeltaTypeBean.EDIT, p_keyLocal.getUserLocal(), nodeLocal.getDBObjectLocal());
                if (!nodeValue.getName().equals(nodeLocal.getName()))
                    changeDeltaLocal.addItem(ChangeDeltaItemTypeBean.NAME, nodeValue.getName());
                if (!nodeValue.getDescription().equals(nodeLocal.getDescription()))
                    changeDeltaLocal.addItem(ChangeDeltaItemTypeBean.DESCRIPTION, nodeValue.getDescription());
                nodeLocal.setNodeValue(nodeValue);
                p_statusLocal.setStatusValue(p_statusValue);
            }
            else
                throw new PermissionDeniedException();
        }
        catch(NamingException e) {
            throw new EJBException(e);
        }
        catch(CreateException e) {
            throw new EJBException(e);
        }
    }
    
    /**
     * If the user has permission to access a status then return the local object.
     *
     * @param p_key User's security key
     * @param p_statusPK The Status object to lookup
     *
     * @ejb.interface-method view-type="local"
     **/
    public StatusLocal getStatusLocal(KeySessionLocal p_keyLocal, 
                                      StatusPK p_statusPK)
        throws PermissionDeniedException, InvalidKeyException {
        try {
            StatusLocal statusLocal = StatusUtil.getLocalHome().findByPrimaryKey(p_statusPK);
            NodeLocal nodeLocal = statusLocal.getNodeLocal();
            if (m_searchSessionLocal.getAssignedPermissionsLocal(p_keyLocal, nodeLocal).contains(m_permissionNodeView)) {
                return statusLocal;
            }
            else
                throw new PermissionDeniedException();
        }
        catch(NamingException e) {
            throw new EJBException(e);
        }
        catch(FinderException e) {
            throw new EJBException(e);
        }
    }
    
    /**
     * Get the full list of status value objects.
     *
     * @param p_key User's security key
     *
     * @ejb.interface-method view-type="local"
     **/
    public com.velocityme.valueobjects.StatusValue[] getAllValueObjects(com.velocityme.interfaces.KeySessionLocal p_keyLocal) throws InvalidKeyException {
        StatusValue[] values = null;
        try {
            if (p_keyLocal.isValid()) {
                Collection statuses = StatusUtil.getLocalHome().findAll();
                values = new StatusValue[statuses.size()];
                int j = 0;
                Iterator i = statuses.iterator();
                while(i.hasNext()) {
                    StatusLocal statusLocal = (StatusLocal)i.next();
                    values[j++] = new StatusValueToString(statusLocal.getStatusValue());
                }
            }
            else
                throw new InvalidKeyException();
        }
        catch(NamingException e) {
            throw new EJBException(e);
        }
        catch(FinderException e) {
            throw new EJBException(e);
        }
        return values;
    }
    
    /**
     * Create the Session Bean.
     * @throws CreateException 
     */
    public void ejbCreate() throws CreateException {
        try {
            m_searchSessionLocal = SearchSessionUtil.getLocalHome().create();
            m_permissionStatusCreate = PermissionUtil.getLocalHome().findByPrimaryKey(new PermissionPK(new Integer(PermissionBean.STATUS_CREATE)));
            m_permissionStatusEdit = PermissionUtil.getLocalHome().findByPrimaryKey(new PermissionPK(new Integer(PermissionBean.STATUS_EDIT)));
            m_permissionNodeView = PermissionUtil.getLocalHome().findByPrimaryKey(new PermissionPK(new Integer(PermissionBean.NODE_VIEW)));
        }
        catch(NamingException e) {
            throw new CreateException(e.getMessage());
        }
        catch(FinderException e) {
            throw new CreateException(e.getMessage());
        }
    }

    public void ejbActivate() throws java.rmi.RemoteException {
    }
    
    public void ejbPassivate() throws java.rmi.RemoteException {
    }
    
    public void ejbRemove() throws java.rmi.RemoteException {
    }
    
    public void setSessionContext(javax.ejb.SessionContext sessionContext) throws java.rmi.RemoteException {
        m_context = sessionContext;
    }
    
}
